package com.hbgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hbgc.mapper.TbTypeMapper;
import com.hbgc.mapper.TbVideoMapper;
import com.hbgc.pojo.TbType;
import com.hbgc.pojo.TbVideo;
import com.hbgc.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements IVideoService {

    @Autowired
    private TbVideoMapper tbVideoMapper;

    @Autowired
    private TbTypeMapper tbTypeMapper;

    @Override
    public PageInfo queryTbVideoByPage(int pageNow, int pageSize, Map searchMap) {
        PageHelper.startPage(pageNow,pageSize);
        List<TbVideo> tbVideos = tbVideoMapper.selectAllByMapList(searchMap);
        PageInfo pageInfo=new PageInfo(tbVideos);
        return pageInfo;
    }

    @Override
    public boolean saveTbVideo(TbVideo tbVideo) {
        return  tbVideoMapper.insert(tbVideo)>0?true:false;
    }

    @Override
    public boolean updateTbVideoById(TbVideo tbVideo) {
        return tbVideoMapper.updateByPrimaryKey(tbVideo)>0?true:false;
    }

    @Override
    public boolean deleteTbVideoById(Long videoid) {
        return tbVideoMapper.deleteByPrimaryKey(videoid)>0?true:false;
    }

    @Override
    public TbVideo getTbVideoById(Long videoid) {
        return tbVideoMapper.selectByPrimaryKey(videoid);
    }

    @Override
    public void clear() {
         tbVideoMapper.clear();
    }

    @Override
    public Map getTbVideoByIndex() {
        Map data=new HashMap();


        //查询全部的电影前10个
        List<TbVideo> top10_videos = tbVideoMapper.selectAllTop10OrderByUpdateTime();
        //查询电影 8个
        List<TbType> typeList = tbTypeMapper.selectALLByPid(1);
        List movieIds=new ArrayList();
        for (TbType type:typeList)
        {
            movieIds.add(type.getTypeid());
        }

        List<TbVideo> movies = tbVideoMapper.selectAllByTypeidTop8ByUpdateTime(movieIds);

        //查询电视剧 8个
        List<TbType> typeList2 = tbTypeMapper.selectALLByPid(3);
        List tvIds=new ArrayList();
        for (TbType type:typeList2)
        {
            tvIds.add(type.getTypeid());
        }
        List<TbVideo> tvs = tbVideoMapper.selectAllByTypeidTop8ByUpdateTime(tvIds);

        //查询综艺8个
        List<TbType> typeList3 = tbTypeMapper.selectALLByPid(6);
        List showIds=new ArrayList();
        if(typeList3!=null&&typeList3.size()==0)
        {
            showIds.add(6);

        }
        List<TbVideo> shows = tbVideoMapper.selectAllByTypeidTop8ByUpdateTime(showIds);


        //查询动漫8个
        List<TbType> typeList4 = tbTypeMapper.selectALLByPid(7);
        List catonIds=new ArrayList();
        if(typeList4!=null&&typeList4.size()==0)
        {
            catonIds.add(7);
        }
        List<TbVideo> catons = tbVideoMapper.selectAllByTypeidTop8ByUpdateTime(catonIds);
        data.put("videoTop10",top10_videos);
        data.put("movies",movies);
        data.put("tvs",tvs);
        data.put("shows",shows);
        data.put("catons",catons);

     return data;

    }


    public PageInfo getTbVideoByTypePid(int pageNow,int pageSize,int typeid,Map search) {

        List<TbType> typeList = tbTypeMapper.selectALLByPid(typeid);
        List ids = new ArrayList();
        if(typeList!=null&&typeList.size()>0) {

            for (TbType type : typeList) {
                ids.add(type.getTypeid());
            }
        }
        else
        {
            ids.add(typeid);
        }
        search.put("ids",ids);
        PageHelper.startPage(pageNow,pageSize);
        List<TbVideo> tbVideos = tbVideoMapper.selectAllByTypeidOrderByUpdateTime(search);
        PageInfo pageInfo=new PageInfo(tbVideos);
        return pageInfo;
    }
}
